package com.sam.demo.rabbitmq.receiver;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.sam.demo.rabbitmq.message.SendData;

@Component
public class Receiver {
	
	private ObjectMapper objectMapper = new ObjectMapper();
	
	@Autowired
	private MessageConverter customConverter;
	/**
	 * 消费者1A监听sam.topicQueue队列
	 * @param msg
	 * @throws JsonProcessingException 
	 */
//	@RabbitListener(queues = "sam.topicQueue")
//    public void processMessageA(SendData2 data) throws JsonProcessingException {
//		System.out.println("消费者A" + Thread.currentThread().getName() + " 接收到来自sam.topicQueue队列的消息：" + objectMapper.writeValueAsString(data));
//		//return msg.toUpperCase();
//	}
//	@RabbitListener(queues = "sam.topicQueue")
//    public void processMessageB(SendData2 data) throws JsonProcessingException {
//		System.out.println("消费者B" + Thread.currentThread().getName() + " 接收到来自sam.topicQueue队列的消息：" + objectMapper.writeValueAsString(data));
//		//return msg.toUpperCase();
//	}
	
	@RabbitListener(queues = "sam.topicQueue0", containerFactory="containerFactory0")
    public void processMessage0(Message msg, Channel channel) throws JsonProcessingException {
		try {
			SendData data = (SendData) customConverter.fromMessage(msg);
			String s = objectMapper.writeValueAsString(data);
			System.out.println("消费者" + Thread.currentThread().getName() + " 接收到来自sam.topicQueue0队列的消息：" + s);
			channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
		} catch (IOException e) {
			System.out.println("消息消费者异常，" + e.getMessage());
			e.printStackTrace(System.out);
		}
	}
	@RabbitListener(queues = "sam.topicQueue1", containerFactory="containerFactory1")
    public void processMessage1(SendData msg) throws JsonProcessingException {
		String s = objectMapper.writeValueAsString(msg);
		System.out.println("消费者" + Thread.currentThread().getName() + " 接收到来自sam.topicQueue1队列的消息：" + s);
	}
	@RabbitListener(queues = "sam.topicQueue2", containerFactory="containerFactory2")
    public void processMessage2(SendData msg) throws JsonProcessingException {
		String s = objectMapper.writeValueAsString(msg);
		System.out.println("消费者" + Thread.currentThread().getName() + " 接收到来自sam.topicQueue2队列的消息：" + s);
	}
	
	/**
	 * -------------------------------------------------------------------------------------------------------
	 */
	
	/**
	 * 消费者2A监听sam.fanoutQueue1队列，该队列的消息平均分给这2个监听器处理
	 * @param msg
	 */
//	@RabbitListener(queues = "sam.fanoutQueue0")
//    public void processMessage2A(SendData2 data) {
//		System.out.println("消费者2A" + Thread.currentThread().getName() + " 接收到来自sam.fanoutQueue0队列的消息：" + data.getContent());
//	}
//	
//	/**
//	 * 消费者2B监听sam.fanoutQueue2队列
//	 * @param msg
//	 */
//	@RabbitListener(queues = "sam.fanoutQueue1")
//    public void processMessage2B(SendData data) {
//		System.out.println("消费者2B" + Thread.currentThread().getName() + " 接收到来自sam.fanoutQueue1队列的消息：" + data.getContent());
//	}
}
